package org.example.netty.test.nio.netty.chat.session;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.bean.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.example.netty.test.nio.netty.chat.common.SystemMessageType.SEND_USER_CHAT_FAIL;

/**
 * @author zp
 * @version 1.0
 * @description: 用户Session 保存用户的所有信息
 * @date 2023/4/4 20:21
 */
@Slf4j
@EqualsAndHashCode
public class UserSession implements Serializable {

    /**
     * @description: 单独聊天的集合
     * @author zp
     * @date: 2023/4/4 20:23
     * @param null
     */
    private final Map<String, UserSession> chatSessionMap = new HashMap<>();
    /**
     * @description: 保存群聊的集合
     * @author zp
     * @date: 2023/4/4 20:23
     * @param null
     */
    private final Set<String> userAddGroupName = new HashSet<>();

    private User user;


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    /**
     * @param chatName
     * @param userSession
     * @return boolean
     * @description: 绑定单独聊天
     * @author zp
     * @date: 2023/4/5 20:02
     */
    public boolean bindChat(String chatName, UserSession userSession) {
        if (chatSessionMap.containsKey(chatName)) {
            return false;
        }
        chatSessionMap.put(chatName, userSession);
        return true;
    }

    /**
     * @return List<String>
     * @description: 获取你加入群聊的群名称
     * @author zp
     * @date: 2023/4/11 20:44
     */
    public Set<String> getGroupNames() {
        log.debug("你已加入的群信息：[{}]",userAddGroupName);
        return userAddGroupName;
    }

    /**
     * @param groupName
     * @return boolean
     * @description: 校验是否已经在群聊里面
     * @author zp
     * @date: 2023/4/11 20:20
     */
    public boolean isInGroup(String groupName) {
        return userAddGroupName.contains(groupName);
    }

    /**
     * @param groupChatName
     * @return boolean
     * @description: 绑定群聊天
     * @author zp
     * @date: 2023/4/5 20:06
     */
    public boolean bindChatGroup(String groupChatName) {
        if (userAddGroupName.contains(groupChatName)) {
            return false;
        }
        log.debug("打印userSession 绑定的群信息 群名称 [{}] ",groupChatName);
        userAddGroupName.add(groupChatName);
        return true;
    }

    /**
     * @param chatId
     * @return boolean
     * @description: 解绑单独聊天
     * @author zp
     * @date: 2023/4/5 20:07
     */
    public boolean unbindChat(String chatId) {
        if (!chatSessionMap.containsKey(chatId)) {
            return false;
        }
        chatSessionMap.remove(chatId);
        return true;
    }

    /**
     * @param sendGroupMessage
     * @description: 发送群消息
     * @author zp
     * @date: 2023/4/14 20:14
     */
    public void sendGroupChatMessage(SendGroupMessage sendGroupMessage) {
        String groupName = sendGroupMessage.getGroupName();
        String message = sendGroupMessage.getMessage();
        String senderName = sendGroupMessage.getSender();

        GlobalContent globalContent = GlobalContent.getInstance();
        Set<UserSession> groupUserSessions = globalContent.getGroupUserSession(groupName);
        System.out.println("打印出 群聊成员："+groupUserSessions);
        if (groupUserSessions != null) {
            UserSession sendUserSession = globalContent.getSession(senderName);
            groupUserSessions.remove(sendUserSession);
            for (UserSession userSession : groupUserSessions) {
                globalContent.getUserChannelById(userSession.user.getUserId()).writeAndFlush(message);
            }
        }
    }

    /**
     * @param groupChatId
     * @return boolean
     * @description: 解绑群聊天
     * @author zp
     * @date: 2023/4/5 20:09
     */
    public boolean unbindChatGroup(String groupChatId) {
        if (!userAddGroupName.contains(groupChatId)) {
            return false;
        }
        userAddGroupName.remove(groupChatId);
        return true;
    }

    /**
     * @description: 获取单独聊天列表
     * @author zp
     * @date: 2023/4/5 20:10
     */
    public Set<String> getChatNameList() {
        return chatSessionMap.keySet();
    }

    /**
     * @param chatName
     * @return UserSession
     * @description: 根据单独聊天名称 获取接收方的UserSession
     * @author zp
     * @date: 2023/4/12 20:08
     */
    public UserSession getUserChatUserSessionList(String chatName) {
        return chatSessionMap.get(chatName);
    }

    /**
     * @return List<String>
     * @description: 获取群聊天的列表
     * @author zp
     * @date: 2023/4/5 20:12
     */
    public Set<String> getChatGroupNameList() {
        return userAddGroupName;
    }

    /**
     * @param createGroupMessage
     * @description: 创建群聊
     * @author zp
     * @date: 2023/4/10 19:47
     */
    public void createGroup(CreateGroupMessage createGroupMessage) {
        String groupName = createGroupMessage.getGroupName();
        userAddGroupName.add(groupName);
    }

    /**
     * @param chatName
     * @description: 删除单独聊天
     * @author zp
     * @date: 2023/4/14 19:26
     */
    public void removeUserChat(String chatName) {
        chatSessionMap.remove(chatName);
    }

    /**
     * @param groupChatName
     * @description: 删除群聊天
     * @author zp
     * @date: 2023/4/14 19:28
     */
    public void removeGroupChat(String groupChatName) {
        userAddGroupName.remove(groupChatName);
    }

    /**
     * @param sendUserMessage
     * @description: 发送单聊信息成功
     * @author zp
     * @date: 2023/4/14 20:24
     */
    public void sendUserChatMessage(SendUserMessage sendUserMessage, ChannelHandlerContext ctx) {
        String recipientUserName = sendUserMessage.getRecipient();
        UserSession userSession = chatSessionMap.get(recipientUserName);
        if(userSession == null || userSession.getUserChatUserSessionList(sendUserMessage.getSender()) == null){
            SystemMessage.sendFailMessage(SEND_USER_CHAT_FAIL,ctx);
        }else {
            GlobalContent globalContent = GlobalContent.getInstance();
            String userId = userSession.getUser().getUserId();
            globalContent.getUserChannelById(userId).writeAndFlush(sendUserMessage);
        }

    }
}
